package com.leo;

import javafx.application.Application;
import javafx.beans.Observable;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import javafx.util.Callback;

/**
 * @ClassName ListViewDemo
 * @Description 五种数据类型得增删改
 * @Author wangss
 * @date 2020.06.09 11:04
 * @Version 1.0
 */
public class ListViewDemo8 extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        AnchorPane an = new AnchorPane();
        an.setStyle("-fx-background-color: #e7eeea");
        Scene scene = new Scene(an);

        ObservableList<PersonProperty> callList = FXCollections.observableArrayList(new Callback<PersonProperty, Observable[]>() {
            @Override
            public Observable[] call(PersonProperty param) {
                SimpleStringProperty[] properties = new SimpleStringProperty[]{param.nameProperty(), param.ageProperty()};
                return properties;
            }
        });

        ListView<PersonProperty> listView = new ListView<>(callList);
        listView.setPlaceholder(new Label("没有数据"));
        listView.setPrefWidth(300d);
        listView.setPrefWidth(300d);

        ObservableList<PersonProperty> list = listView.getItems();
        list.add(new PersonProperty("A", "1"));
        list.add(new PersonProperty("B", "2"));
        list.add(new PersonProperty("C", "3"));
        list.add(new PersonProperty("D", "4"));

        Button button = new Button("button");

        an.getChildren().addAll(button, listView);

        AnchorPane.setTopAnchor(listView, 50d);
        AnchorPane.setLeftAnchor(listView, 100d);

        primaryStage.setScene(scene);
        primaryStage.setTitle("ListView");
        primaryStage.setWidth(600d);
        primaryStage.setHeight(600d);
        primaryStage.setResizable(false);
        primaryStage.show();

        listView.setEditable(true);

        listView.setCellFactory(new Callback<ListView<PersonProperty>, ListCell<PersonProperty>>() {

            PersonProperty personProperty = null;

            @Override
            public ListCell<PersonProperty> call(ListView<PersonProperty> param) {

                // 这里需要设置监听，以获取索引
                param.setOnEditStart(event -> {
                    int index = event.getIndex();
                    personProperty = param.getItems().get(index);
                });

                ListCell<PersonProperty> listCell = new ListCell<PersonProperty>() {

                    @Override
                    public void startEdit() {
                        super.startEdit();
                        HBox hBox = new HBox(10);

                        hBox.setAlignment(Pos.CENTER_LEFT);

                        ImageView iv = new ImageView("file:D:\\图片保存\\小图片\\1.jpg");
                        iv.setPreserveRatio(false);
                        iv.setFitHeight(20d);
                        iv.setFitWidth(20d);

                        Button button1 = new Button(personProperty.getName() + "button");

                        TextField t1 = new TextField(personProperty.getName());
                        t1.setPrefWidth(30d);

                        TextField t2 = new TextField(personProperty.getAge());
                        t2.setPrefWidth(30d);

                        hBox.getChildren().addAll(iv, button1, t1, t2);

                        this.setGraphic(hBox);

                        t1.setOnKeyPressed(event -> {
                            if (event.getCode().getName().equals(KeyCode.ENTER.getName())) {
                                if (t1.getText().equals("")) {
                                    this.commitEdit(personProperty);
                                } else {
                                    personProperty.setAge(t2.getText());
                                    personProperty.setName(t1.getText());
                                    this.commitEdit(personProperty);
                                }
                            }
                        });

                        t2.setOnKeyPressed(event -> {
                            if (event.getCode().getName().equals(KeyCode.ENTER.getName())) {
                                if (t2.getText().equals("")) {
                                    this.commitEdit(personProperty);
                                } else {
                                    personProperty.setAge(t2.getText());
                                    personProperty.setName(t1.getText());
                                    this.commitEdit(personProperty);
                                }
                            }
                        });

                    }

                    @Override
                    public void cancelEdit() {
                        super.cancelEdit();

                        HBox hBox = new HBox(10);

                        hBox.setAlignment(Pos.CENTER_LEFT);

                        ImageView iv = new ImageView("file:D:\\图片保存\\小图片\\1.jpg");
                        iv.setPreserveRatio(false);
                        iv.setFitHeight(20d);
                        iv.setFitWidth(20d);

                        Button button1 = new Button(personProperty.getName() + "button");

                        Label label = new Label(personProperty.getName());
                        Label label1 = new Label(personProperty.getAge());

                        hBox.getChildren().addAll(iv, button1, label, label1);

                        this.setGraphic(hBox);

                    }

                    @Override
                    protected void updateItem(PersonProperty item, boolean empty) {
                        super.updateItem(item, empty);

                        if (!empty) {
                            HBox hBox = new HBox(10);

                            hBox.setAlignment(Pos.CENTER_LEFT);

                            ImageView iv = new ImageView("file:D:\\图片保存\\小图片\\1.jpg");
                            iv.setPreserveRatio(false);
                            iv.setFitHeight(20d);
                            iv.setFitWidth(20d);

                            Button button1 = new Button(item.getName() + "button");

                            Label label = new Label(item.getName());
                            Label label1 = new Label(item.getAge());

                            hBox.getChildren().addAll(iv, button1, label, label1);

                            this.setGraphic(hBox);
                        }
                    }
                };
                return listCell;
            }
        });
    }
}
